<template>
  <!-- 新增 搜索 -->
  <div>
    <!-- 表单 -->
    <TopFilter ref="formlist" :cols="3" :list="list" :rows="2" @filterChange="formChange" @resetChange="resetFun"></TopFilter>
    <!-- 表格 -->
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="partPartOutboundRepairMaterialsComponentsSearchPart"
      :fetchapi="fetchApi"
      :params="fetchParams"
      height="auto"
      selectionType="single"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onPageChange="onPageChange"
      :onEnterEvent="onEnterEvent"
    ></FilterTable>
    <!-- 替换件查看弹窗 -->
    <BaseDialog :visible.sync="replacePartVisible" title="查看替换件" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <!-- <replaceAndInventory :partInfo="visibleReplacePartInfo"></replaceAndInventory> -->
    </BaseDialog>
    <!-- :onEnterEvent="onEnterEvent" -->
  </div>
</template>

<script>
import { state, mutations } from './../store';
import util from '../common/util';
import { dictionary } from '@/utils/dictMixin';
import { isAuthorized } from '@/utils/index';
import { queryPartInfo, queryPartStock } from '@/api/part/partOutbound/repairMaterials/index.js';
import { findAllRepair } from '@/api/repair/basicParameters';
import { getbrandsdictList } from '@/api/part/partOutbound/partBreak';
import { getStorageList } from '@/api/part/partOutbound/repairMaterials/dict';
import { getBrand } from '@/api/repair/basicParameters';
// import replaceAndInventory from '@/pages/part/report/replaceAndInventory';
export default {
  name: 'cgrk_part_search',
  components: {
    // replaceAndInventory
  },
  props: {
    storageType: {
      type: String
    }
  },
  mixins: [dictionary],
  data() {
    return {
      list: this.createList(),
      columns: this.createColumns(),
      fetchApi: queryPartInfo,
      fetchParams: { xhrAbort: true },
      visibleReplacePartInfo: {}, // 查看替代件的配件信息
      replacePartVisible: false, // 是否显示替代配件查看弹窗
      initStorage: ''
    };
  },
  mounted() {
    console.log('cgrk_part_search mounted');
    window.cgrk_part_search = this;
    this.getBrands();
    // this.findAllRepair();
    this.initSelect();
    this.getSystemAuthority();
  },
  methods: {
    createList() {
      return [
        {
          type: 'SELECT',
          label: '仓库名称',
          fieldName: 'storageCode',
          // itemList: state.dict_ck
          itemList: []
        },
        // {
        //   type: 'SELECT',
        //   label: '品牌',
        //   fieldName: 'brand',
        //   itemList: []
        // },
        // {
        //   type: 'SELECT',
        //   label: '配件类别',
        //   fieldName: 'dropdown',
        //   itemList: this.createDictList(8075)
        // },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          autofocus: true
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName'
        },

        {
          type: 'INPUT',
          label: '库位',
          fieldName: 'storagePositionCode'
        }
        // {
        //   type: 'INPUT',
        //   label: '备注',
        //   fieldName: 'remark'
        // }
      ];
    },
    createColumns() {
      return [
        // 序号
        {
          title: '序号',
          sorter: true,
          width: 70,
          dataIndex: 'xuhao',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },

        // 仓库
        {
          title: '仓库名称',
          sorter: true,
          dataIndex: 'STORAGE_NAME'
        },
        // 库位
        {
          title: '库位',
          sorter: true,
          dataIndex: 'STORAGE_POSITION_CODE'
        },
        // 配件代码
        {
          title: '配件代码',
          sorter: true,
          dataIndex: 'PART_NO',
          showOverflowTooltip: true
        },
        // 配件名称
        {
          title: '配件名称',
          sorter: true,
          dataIndex: 'PART_NAME',
          showOverflowTooltip: true
        },
        // 销售价
        {
          title: '终端建议零售VIP价格',
          sorter: true,
          dataIndex: 'INSTRUCT_PRICE',
          precision: 2
        },
        // 成本价
        {
          title: '含税成本价',
          sorter: true,
          dataIndex: 'COST_PRICE',
          precision: 2,
          hidden: true
        },
        {
          title: '发料数量',
          sorter: true,
          dataIndex: 'PART_QUANTITY',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          precision: 2
        },
        {
          title: '可用库存',
          sorter: true,
          dataIndex: 'USEABLE_QUANTITY',
          precision: 2
        },
        // 账面库存
        {
          title: '账面库存',
          sorter: true,
          dataIndex: 'STOCK_QUANTITY',
          precision: 2
        },

        // 借进数量
        // {
        //   title: '借进数量',
        //   sorter: true,
        //   dataIndex: 'BORROW_QUANTITY',
        //   precision: 2
        // },
        // 借出数量
        // {
        //   title: '借出数量',
        //   sorter: true,
        //   dataIndex: 'LEND_QUANTITY',
        //   precision: 2
        // },
        // 备注

        {
          title: '备注',
          sorter: true,
          dataIndex: 'REMARK'
        },
        {
          title: '替代配件',
          sorter: true,
          dataIndex: 'OPTION_NO'
        }
      ];
    },
    async findAllRepair() {
      const res = await findAllRepair();
      this.initStorage = this.list.find(x => x.fieldName === 'storageCode').initialValue = res.data['1100'];
    },

    async initSelect() {
      // let res = await getbrandsdictList();
      // let res = await getBrand();
      // this.list.find(x => x.fieldName === 'brand').itemList = res.data.map(x => ({ text: x.BRAND_NAME, value: x.BRAND_CODE + '' }));
      // this.list.find(x => x.fieldName === 'brand').initialValue = res.data.find(x => x.BRAND_NAME == '奥迪').BRAND_CODE;
    },
    async getBrands() {
      const res = await getStorageList();
      this.list.find(x => x.fieldName === 'storageCode').itemList = res.data
        .filter(x => {
          return x.STORAGE_TYPE == this.storageType;
        })
        .map(x => {
          return { value: x.STORAGE_CODE, text: x.STORAGE_NAME };
        });
    },

    formChange(p) {
      console.log('formListChange', p);
      this.fetchParams = Object.assign({ storageType: this.storageType }, p);
    },
    resetFun() {
      // this.$nextTick(() => {
      //   this.list.find(x => x.fieldName == 'storageCode').initialValue = this.initStorage;
      // });
    },
    onPageChange() {
      console.log('onPageChange');
    },
    async onEnterEvent(row) {
      // 双击选中  整合配件信息
      console.log('onEnterEvent', row);
      this.$emit('dblclickPartInfo', Object.assign({}, row));
    },
    handleCheckReplacePart(row) {
      let data = util.camelCaseObject(row);
      console.log('传给组件', row, data);
      this.visibleReplacePartInfo = data;
      this.replacePartVisible = true;
    },
    // 权限
    getSystemAuthority() {
      let costPriceFlag = isAuthorized('1313', '13131005');
      if (costPriceFlag) {
        this.columns.find(x => x.dataIndex === 'COST_PRICE').hidden = false;
      }
    }
  }
};
</script>

<style></style>
